package com.qianyu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.entity.*;
import com.qianyu.project.mapper.DeviceRequirePlanMapper;
import com.qianyu.project.service.IAttachemenInfoService;
import com.qianyu.project.service.IDeviceRequireListService;
import com.qianyu.project.service.IDeviceRequirePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.service.IMaterialInfoService;
import com.qianyu.project.utils.PageUtil;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备需求计划 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Transactional
@Service
public class DeviceRequirePlanServiceImpl extends ServiceImpl<DeviceRequirePlanMapper, DeviceRequirePlan> implements IDeviceRequirePlanService {

    @Autowired
    private DeviceRequirePlanMapper deviceRequirePlanMapper;
    @Autowired
    private IAttachemenInfoService attachemenInfoService;
    @Autowired
    private IDeviceRequireListService deviceRequireListService;

    @Override
    @FlowBegin("device_require_plan")
    public Integer Mysave(@FlowEntry("id") DeviceRequirePlan dd , Map<String, Object> params) {

        deviceRequirePlanMapper.insert(dd);

        List<Map<String, Object>> mapEs = (List<Map<String, Object>>) params.get("enclosures");
        List<AttachemenInfo> aiList = Lists.newArrayList();
        mapEs.forEach(i->{
            AttachemenInfo ai = new AttachemenInfo();
            ai.setFileName(i.get("fileName")+"");
            String fileName = i.get("fileName") + "";
//            ai.setFileType(fileName.split(".")[1]);
            ai.setFilePath(i.get("filePath")+"");
            ai.setUploadDate(i.get("time")+"");
            ai.setRelativeId(dd.getId());
            aiList.add(ai);
        });
        List<Integer> Allid = Lists.newArrayList();
        attachemenInfoService.saveBatch(aiList);
        aiList.forEach(i->{
            Allid.add(i.getId());
        });
        if(aiList.size()!=0){
            String attid = "";
            for (int i = 0; i< Allid.size(); i++){
                if (Allid.size()-1==i){
                    attid+=Allid.get(i);
                    continue;
                }
                attid+=Allid.get(i)+",";
            }
            DeviceRequirePlan d = new DeviceRequirePlan();
            d.setId(dd.getId());
            d.setAttachIds(attid);
            deviceRequirePlanMapper.updateById(d);
        }

        List<Map<String, String>> mapMi = (List<Map<String, String>>) params.get("materialInfos");
        List<DeviceRequireList> MiList = Lists.newArrayList();
        mapMi.forEach(i->{
            DeviceRequireList dml = new DeviceRequireList();
            dml.setDPlanId(dd.getId());
            dml.setMaterialId(Integer.valueOf(i.get("materialId")+""));
            dml.setUseCount(Integer.valueOf(i.get("useCount")+""));
            dml.setComment(i.get("comment")+"");
            MiList.add(dml);
        });
        deviceRequireListService.saveBatch(MiList);

        return 1;
    }

    @Override
    public Integer getAddId(String year) {
        Map<String, Integer> addId = deviceRequirePlanMapper.getAddId(year);
        Integer d = Integer.valueOf(addId.get("id"));
        Integer id;
       if(d<=0 || d==null){
           id=Integer.valueOf(year)+Integer.valueOf("0000");
       }else{
           id=d;
       }
        return id+1;
    }

    @Override
    public IPage<DeviceRequirePlan> Mylist(Map<String, String> params) {
        IPage<DeviceRequirePlan> page = PageUtil.createPage(params);
        return deviceRequirePlanMapper.Mylist(page,params);
    }

    @Override
    public List<DeviceRequirePlan> listNopage(Map<String, String> params) {
        return deviceRequirePlanMapper.listNopage(params);
    }

    @Override
    public void Myupdate(DeviceRequirePlan dd ,Map<String, Object> params) {
        List<Map<String, Object>> mapEs = (List<Map<String, Object>>) params.get("enclosures");
        List<AttachemenInfo> aiList = Lists.newArrayList();
        mapEs.forEach(i->{
            AttachemenInfo ai = new AttachemenInfo();
            ai.setId(Integer.valueOf(i.get("id")+""));
            ai.setFileName(i.get("fileName")+"");
            String fileName = i.get("fileName") + "";
//            ai.setFileType(fileName.split(".")[1]);
            ai.setFilePath(i.get("filePath")+"");
            ai.setUploadDate(i.get("time")+"");
            ai.setRelativeId(dd.getId());
            aiList.add(ai);
        });
        List<Map<String, String>> mapMi = (List<Map<String, String>>) params.get("materialInfos");
        List<DeviceRequireList> MiList = Lists.newArrayList();
        mapMi.forEach(i->{
            DeviceRequireList dml = new DeviceRequireList();
            dml.setId(Integer.valueOf(i.get("id")+""));
            dml.setDPlanId(dd.getId());
            dml.setMaterialId(Integer.valueOf(i.get("materialId")+""));
            dml.setUseCount(Integer.valueOf(i.get("useCount")+""));
            dml.setComment(i.get("comment")+"");
            MiList.add(dml);
        });
        attachemenInfoService.saveOrUpdateBatch(aiList);
        deviceRequireListService.saveOrUpdateBatch(MiList);

    }
}
